import time

import pandas as pd
from sqlalchemy import create_engine
import os
import requests


#   建立与mysql的数据库的连接
connect=create_engine('mysql+mysqlconnector://root:123456@192.168.67.193:3307/source?ssl_disabled=True&charset=utf8mb4')

#   使用sql直接读取数据
data=pd.read_sql('select * from shop_info',connect)

url_list=data['image_url'].to_list()


img_list=[]
imgextra_list=[]
minolta_list=[]
bao_list=[]
#   将img和imgextra还有minolta路径的分开
for x in url_list:
    if "imgextra" in str(x):
        imgextra_list.append(x)
    elif "minolta" in str(x):
        minolta_list.append(x)
    elif "bao" in str(x):
        bao_list.append(x)
    else:
        img_list.append(x)

headers = {
   'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
}

# for x in img_list:
#     #   先将中间的路径和图片的名字得到
#     end_path=os.path.basename(x)
#     middle_path = x.split("img/")[1].rstrip(end_path).replace("/","\\")           #rstrip()删除末尾的....
#     # print(middle_path)
#     #   然后创建路径
#     save_dir=os.path.join(r'E:\images\img',middle_path)
#     #   保存的文件位置
#     save_path=os.path.join(save_dir,end_path)
#     # print(save_path)
#     #   如果目录不存在则创建目录
#     if not os.path.exists(save_dir):
#         os.makedirs(save_dir)
#     #   使用request下载图片
#     response=requests.get(x,headers=headers)
#     if response.status_code == 200:
#         #   将内容写进图片
#         with open(save_path, 'wb') as file:
#             file.write(response.content)
#     else:
#         print(f"下载失败: {x}, 状态码: {response.status_code}")
#     time.sleep(1)

print("img采集完毕")

# for x in imgextra_list:
#     #   先将中间的路径和图片的名字得到
#     end_path=os.path.basename(x)
#     middle_path = x.split("imgextra/")[1].rstrip(end_path).replace("/","\\")           #rstrip()删除末尾的....
#     # print(middle_path)
#     #   然后创建路径
#     save_dir=os.path.join(r'E:\images\imgextra',middle_path)
#     #   保存的文件位置
#     save_path=os.path.join(save_dir,end_path)
#     # print(save_path)
#     #   如果目录不存在则创建目录
#     if not os.path.exists(save_dir):
#         os.makedirs(save_dir)
#     #   使用request下载图片
#     response=requests.get(x,headers=headers)
#     if response.status_code == 200:
#         #   将内容写进图片
#         with open(save_path, 'wb') as file:
#             file.write(response.content)
#     else:
#         print(f"下载失败: {x}, 状态码: {response.status_code}")
#     time.sleep(1)

print("imgextra采集完毕")

for x in minolta_list:
    if x=='https://ilce.alicdn.com/minolta/475831/0/714ac40ab9537da684bacd07a58b9a18.jpg_800x800.jpg?channel=4&content=%7B%2218%22%3A%7B%22url%22%3A%22O1CN01PuGlBB1DBZTJWMEPA_%21%211785240178.jpg%22%2C%22filters%22%3A%5B%7B%22type%22%3A%22copy%22%2C%22attrs%22%3A%7B%22src_rect%22%3A%5B5%2C114%2C784%2C785%5D%2C%22dst_rect%22%3A%5B0%2C0%2C513%2C513%5D%7D%7D%5D%7D%7D&getAvatar=avatar_580x580q90.jpg_.webp':
        url='https://ilce.alicdn.com/minolta/475831/0/714ac40ab9537da684bacd07a58b9a18.jpg_800x800.jpg?channel=4&content=%7B%2218%22%3A%7B%22url%22%3A%22O1CN01PuGlBB1DBZTJWMEPA_%21%211785240178.jpg%22%2C%22filters%22%3A%5B%7B%22type%22%3A%22copy%22%2C%22attrs%22%3A%7B%22src_rect%22%3A%5B5%2C114%2C784%2C785%5D%2C%22dst_rect%22%3A%5B0%2C0%2C513%2C513%5D%7D%7D%5D%7D%7D&getAvatar=avatar_580x580q90.jpg_.webp'
        #   先将中间的路径和图片的名字得到
        first_end_path=os.path.basename(x)
        end_path ="r1.jpg_.webp"
        middle_path = x.split("minolta/")[1].rstrip(first_end_path).replace("/", "\\")  # rstrip()删除末尾的....
        # print(middle_path)
        #   然后创建路径
        save_dir = os.path.join(r'E:\images\minolta', middle_path)
        #   保存的文件位置
        save_path = os.path.join(save_dir, end_path)
        # print(save_path)
        #   如果目录不存在则创建目录
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        #   使用request下载图片
        response = requests.get(url,headers=headers)
        if response.status_code == 200:
            #   将内容写进图片
            with open(save_path, 'wb') as file:
                file.write(response.content)
        else:
            print(f"下载失败: {x}, 状态码: {response.status_code}")
        time.sleep(1)
        continue
    else:
        #   先将中间的路径和图片的名字得到
        url='https://ilce.alicdn.com/minolta/483209/3/271fb1c2b9a7fd764be84dd57b36e035.jpg_800x800.jpg?channel=4&content=%7B%2211%22%3A%7B%22url%22%3A%22https%3A%2F%2Fimg.alicdn.com%2Fbao%2Fuploaded%2Fi3%2F63788954%2FO1CN01LjxO6I2G0zM02FYmp_%21%2163788954.jpg%22%2C%22filters%22%3A%5B%7B%22type%22%3A%22copy%22%2C%22attrs%22%3A%7B%22src_rect%22%3A%5B2%2C134%2C741%2C741%5D%2C%22dst_rect%22%3A%5B0%2C0%2C513%2C513%5D%7D%7D%5D%7D%7D&getAvatar=avatar_580x580q90.jpg_.webp'
        first_end_path=os.path.basename(x)
        end_path ="r2.jpg_.webp"
        middle_path = x.split("minolta/")[1].rstrip(first_end_path).replace("/", "\\")  # rstrip()删除末尾的....
        # print(middle_path)
        #   然后创建路径
        save_dir = os.path.join(r'E:\images\minolta', middle_path)
        #   保存的文件位置
        save_path = os.path.join(save_dir, end_path)
        # print(save_path)
        #   如果目录不存在则创建目录
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        #   使用request下载图片
        response = requests.get(url,headers=headers)
        if response.status_code == 200:
            #   将内容写进图片
            with open(save_path, 'wb') as file:
                file.write(response.content)
        else:
            print(f"下载失败: {x}, 状态码: {response.status_code}")
        time.sleep(1)
        continue


print("minolta采集完毕")


# for x in bao_list:
#     #   先将中间的路径和图片的名字得到
#     end_path=os.path.basename(x)
#     middle_path = x.split("bao/")[1].rstrip(end_path).replace("/","\\")           #rstrip()删除末尾的....
#     # print(middle_path)
#     #   然后创建路径
#     save_dir=os.path.join(r'E:\images\bao',middle_path)
#     #   保存的文件位置
#     save_path=os.path.join(save_dir,end_path)
#     # print(save_path)
#     #   如果目录不存在则创建目录
#     if not os.path.exists(save_dir):
#         os.makedirs(save_dir)
#     #   使用request下载图片
#     response=requests.get(x,headers=headers)
#     if response.status_code == 200:
#         #   将内容写进图片
#         with open(save_path, 'wb') as file:
#             file.write(response.content)
#     else:
#         print(f"下载失败: {x}, 状态码: {response.status_code}")
#     time.sleep(1)


print("bao采集完毕,全部采集完成")







